package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    //新增部门
    @Override
    public void addDept(Dept dept) {
        //补全Json中缺少的字段
        // 添加创建时间
        dept.setCreateTime(LocalDateTime.now());
        //添加更新时间
        dept.setUpdateTime(LocalDateTime.now());
        //调用持久层方法insert
        deptMapper.insert(dept);
    }
   // 条件分页查询部门
    @Override
    public PageResult<Dept> selectlist(String name, Integer status, Integer page, Integer pageSize) {
        //1、设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);
        //2、调用mapper接口查询
        List<Dept> depts = deptMapper.selectlist(name, status);
        //3、解析并封装分页结果
        Page<Dept> p = (Page<Dept>) depts;
        return new PageResult<>(p.getTotal(), p.getResult());
    }
    //根据id查询部门
    @Override
    public Dept getById(Integer id) {
        return deptMapper.selectById(id);
    }
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
    //根据id删除部门
    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }
// 查询所有部门
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }
}

